using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Services
{
    /// <summary>
    /// 用户设备服务实现
    /// 提供用户设备管理的业务逻辑实现，包括设备信息的查询、添加、更新和删除操作
    /// </summary>
    public class UserDeviceService : IUserDeviceAppService
    {
        /// <summary>
        /// 用户设备仓储接口
        /// </summary>
        private readonly Domain.Interfaces.IUserDeviceRepository _repo;
        
        /// <summary>
        /// 初始化用户设备服务实例
        /// </summary>
        /// <param name="repo">用户设备仓储接口实例</param>
        public UserDeviceService(Domain.Interfaces.IUserDeviceRepository repo)
        {
            _repo = repo;
        }
        
        /// <summary>
        /// 获取所有用户设备记录
        /// 检索系统中的所有用户设备信息并转换为DTO格式
        /// </summary>
        /// <returns>用户设备DTO列表</returns>
        public async Task<IEnumerable<Application.DTOs.UserDeviceDto>> GetAllAsync()
        {
            var entities = await _repo.GetAllAsync();
            return entities.Select(e => new Application.DTOs.UserDeviceDto
            {
                Id = e.Id,
                UserId = e.UserId,
                DeviceName = e.DeviceName,
                DeviceType = e.DeviceType,
                IsTrusted = e.IsTrusted,
                RegisteredAt = e.FirstSeenAt
            });
        }
        
        /// <summary>
        /// 根据ID获取用户设备记录
        /// 查找指定标识符的用户设备详细信息
        /// </summary>
        /// <param name="id">用户设备标识符</param>
        /// <returns>用户设备DTO，如果不存在则返回null</returns>
        public async Task<Application.DTOs.UserDeviceDto?> GetByIdAsync(long id)
        {
            var entity = await _repo.GetByIdAsync(id);
            if (entity == null) return null;
            return new Application.DTOs.UserDeviceDto
            {
                Id = entity.Id,
                UserId = entity.UserId,
                DeviceName = entity.DeviceName,
                DeviceType = entity.DeviceType,
                IsTrusted = entity.IsTrusted,
                RegisteredAt = entity.FirstSeenAt
            };
        }
        
        /// <summary>
        /// 添加新的用户设备记录
        /// 创建新的用户设备信息并保存到数据存储
        /// </summary>
        /// <param name="dto">用户设备DTO对象</param>
        /// <returns>添加操作的异步任务</returns>
        public async Task AddAsync(Application.DTOs.UserDeviceDto dto)
        {
            var entity = new Domain.Entities.UserDevice
            {
                Id = dto.Id,
                UserId = dto.UserId,
                DeviceName = dto.DeviceName,
                DeviceType = dto.DeviceType,
                IsTrusted = dto.IsTrusted,
                FirstSeenAt = dto.RegisteredAt
            };
            await _repo.AddAsync(entity);
        }
        
        /// <summary>
        /// 更新用户设备记录
        /// 更新指定用户设备的信息
        /// </summary>
        /// <param name="id">用户设备标识符</param>
        /// <param name="dto">更新后的用户设备DTO对象</param>
        /// <returns>更新操作的异步任务</returns>
        public async Task UpdateAsync(long id, Application.DTOs.UserDeviceDto dto)
        {
            var entity = new Domain.Entities.UserDevice
            {
                Id = dto.Id,
                UserId = dto.UserId,
                DeviceName = dto.DeviceName,
                DeviceType = dto.DeviceType,
                IsTrusted = dto.IsTrusted,
                FirstSeenAt = dto.RegisteredAt
            };
            await _repo.UpdateAsync(id, entity);
        }
        
        /// <summary>
        /// 删除用户设备记录
        /// 从系统中移除指定的用户设备信息
        /// </summary>
        /// <param name="id">用户设备标识符</param>
        /// <returns>删除操作的异步任务</returns>
        public async Task DeleteAsync(long id)
        {
            await _repo.DeleteAsync(id);
        }
    }
}
